﻿using System.Collections.Generic;
using GameLibrary;
using Microsoft.Xna.Framework;
using Rescue_of_the_Dwarvenzombies.Rendering;
using Rescue_of_the_Dwarvenzombies.Rendering.RenderStates;
using Rescue_of_the_Dwarvenzombies.SceneLayers;
using Rescue_of_the_Dwarvenzombies.SceneObjects.GameObjects;
using Rescue_of_the_Dwarvenzombies.SceneObjects.GUIObjects;

namespace Rescue_of_the_Dwarvenzombies.GameStates
{
    /// <summary>
    /// PauseState is the activated state when any player presses pause.
    /// </summary>
    /// <remarks></remarks>
    internal class PauseState : GameState
    {
        private GUIHandler _guiHandler;

        /// <summary>
        /// Initializes a new instance of the <see cref="PauseState"/> class.
        /// </summary>
        /// <param name="game">The game.</param>
        /// <param name="gameStateManager">The game state manager.</param>
        /// <param name="rootLayer">The root layer.</param>
        /// <remarks></remarks>
        public PauseState(Game game, GameStateManager gameStateManager, ISceneLayer rootLayer /*, GameState parentGameState*/)
            : base(game)
        {
            GameStateManager = gameStateManager;
            RootLayer = rootLayer;
            _guiHandler = new GUIHandler(game);
        }

        /// <summary>
        /// List of GUI objects.
        /// </summary>
        /// <returns></returns>
        /// <remarks></remarks>
        public override List<GameObject> GUIObjects()
        {
            return new List<GameObject>();
        }

        /// <summary>
        /// Updates the specified game time.
        /// </summary>
        /// <param name="gameTime">The game time.</param>
        /// <remarks></remarks>
        public override void Update(GameTime gameTime)
        {
            HasStateChanged();
        }

        /// <summary>
        /// Inits the root layer.
        /// </summary>
        /// <remarks></remarks>
        protected override sealed void InitRootLayer()
        {
        }

        /// <summary>
        /// Determines whether [has state changed].
        /// </summary>
        /// <returns><c>true</c> if [has state changed]; otherwise, <c>false</c>.</returns>
        /// <remarks></remarks>
        protected override bool HasStateChanged()
        {
            IInputManager inputManager = (IInputManager)Game.Services.GetService(typeof(IInputManager));

            //If users have unpaused
            if (inputManager.CheckInput() == Constant.Action.Pause)
            {
                //Pop current state
                GameStateManager.ChangeState(this);

                //Get the render state manager
                RenderStateManager renderStateManager =
                    ((IRenderManager)Game.Services.GetService(typeof(IRenderManager))).RenderStateManager;

                //Pop current render state
                renderStateManager.ChangeState(renderStateManager.CurrentState);

                return true;
            }
            return false;
        }
    }
}